package com.yima.baseproj.utils.random_num;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yima.baseproj.module.sys.kindergarten.KindergartenEntity;
import com.yima.baseproj.module.sys.kindergarten.KindergartenService;
import com.yima.baseproj.module.sys.teacher.TeacherEntity;
import com.yima.baseproj.module.sys.teacher.TeacherService;
import com.yima.baseproj.module.sys.user.SysUserEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.PostConstruct;
import java.security.SecureRandom;
import java.util.*;
@Component
public class RandomUtil {

    //数字+字母
    private static final String ALPHA_NUMERIC_STRING = "0123456789abcdefghijkmnopqrstuvwxyz";


    @Autowired
    private  KindergartenService kgService ;
    @Autowired
    private static KindergartenService kindergartenService;

    @Autowired
    private TeacherService tService ;
    @Autowired
    private static TeacherService teacherService;

    @PostConstruct
    public void init(){
        kindergartenService = kgService;
        teacherService = tService;
    }






    public static String generateUniqueCode(int length) {
        List<Character> characters = new ArrayList<>();
        //生成长度 length
        for (int i = 0; i < length; i++) {
            int randomInt = (int) (Math.random() * ALPHA_NUMERIC_STRING.length());
            characters.add(ALPHA_NUMERIC_STRING.charAt(randomInt));
        }
        //随机排序
        Collections.shuffle(characters);

        StringBuilder sb = new StringBuilder(characters.size());
        for (Character character : characters) {
            sb.append(character);
        }
        return sb.toString();
    }
    //判断幼儿园码是否唯一
    public static boolean contains(String code){
        //从数据库中读取
        KindergartenEntity kg = kindergartenService.checkKgCodeUnique(code);
        //重复
        if(!Objects.isNull(kg)){
            return true;
        }else {
            return false;
        }
    }

    //判断老师码是否唯一
    public static boolean contain(String code){
        //从数据库中读取
        TeacherEntity kg = teacherService.checkTeacherCodeUnique(code);
        //重复
        if(!Objects.isNull(kg)){
            return true;
        }else {
            return false;
        }
    }

    public static String getKgCode(){

        String code = generateUniqueCode(5);
        //判断如果重复
        while(contains(code)){
            //重复则重新生成（保证码的唯一）
            code = generateUniqueCode(5);
        }
        //不重复
        return code;
    }

    public static String getTeacherCode(){

        String code = generateUniqueCode(5);
        //判断如果重复
        while(contain(code)){
            //重复则重新生成（保证码的唯一）
            code = generateUniqueCode(5);
        }
        //不重复
        return code;
    }



}
